/* Filename: susp_rt.c
 * Description:
 * $ ./susp_rt
 * # kill -40 `pidof susp_rt`
 * Last modified: humble 20200408 23:06
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define MYRTSIG (SIGRTMIN+6)

static void usage(void);

void myrtsig_handler(int i)
{
    write(1, "!", 1);
}

int main(int argc, char **argv)
{
    int i;
    sigset_t set, oset, saveset;

    if(argc != 1){
        fprintf(stderr, "argc !=1");
        usage();
        exit(1);
    }

    signal(MYRTSIG, myrtsig_handler);
    sigemptyset(&set);
    sigaddset(&set, MYRTSIG);
    sigprocmask(SIG_UNBLOCK, &set, &saveset);//12
    sigprocmask(SIG_BLOCK, &set, &oset);//2
    while(1){
        for(i = 0; i < 5; i++)
        {
            write(1, "*", 1);
            sleep(1);
        }
        write(1, "\n", 1);

        sigsuspend(&oset);//原子化 unblock后立刻进入pause(wait)
    }
    sigprocmask(SIG_SETMASK, &saveset, NULL);//1
    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
